#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
串口检测工具
用于检测当前系统的可用串口
"""

import serial.tools.list_ports

def detect_ports():
    """检测所有可用串口"""
    print("=== 串口检测工具 ===")
    ports = list(serial.tools.list_ports.comports())
    
    if not ports:
        print("❌ 未检测到任何串口设备")
        return None
    
    print(f"✅ 检测到 {len(ports)} 个串口设备:")
    print()
    
    # Arduino相关关键词
    arduino_keywords = ['Arduino', 'USB Serial', 'CH340', 'CP210', 'FTDI', 'Silicon Labs']
    likely_arduino = []
    
    for i, port in enumerate(ports, 1):
        print(f"{i}. 端口: {port.device}")
        print(f"   描述: {port.description}")
        print(f"   硬件ID: {port.hwid}")
        
        # 检查是否可能是Arduino
        for keyword in arduino_keywords:
            if keyword.lower() in port.description.lower():
                likely_arduino.append(port.device)
                print(f"   ⭐ 可能是Arduino设备")
                break
        
        print()
    
    if likely_arduino:
        print("🎯 推荐的Arduino端口:")
        for port in likely_arduino:
            print(f"   - {port}")
    
    return ports

if __name__ == "__main__":
    detect_ports()
